package com.example.beikeyigou.setting;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.dialog.EditDialog;
import com.example.beikeyigou.login_reg.LoginRegActivity;
import com.example.beikeyigou.login_reg.SelfDataChangeActivity;
import com.example.beikeyigou.login_reg.UserBean;
import com.example.beikeyigou.utils.BaseActivity;
import com.example.beikeyigou.utils.SaveUserUtils;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class SettingActivity extends BaseActivity {
    private ImageView backImg, userImg;
    private TextView userNameTv, userIdTv;

    UserBean.DataBean userData, newUserData;

    String imgPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        findView();
        refreshData();
        setListener();
        changeStatusBarTextImgColor(true);
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    private void refreshData() {
        userData = SaveUserUtils.getUserData(this);
        if (userData == null) {
            userNameTv.setText("请先登录");
            userIdTv.setText("贝壳账号");
            return;
        }
        newUserData = new UserBean.DataBean(userData.getId(), userData.getPhone(),
                userData.getPassword(), userData.getName(), userData.getAddress(),
                userData.getNickname(), userData.getQuestion(), userData.getAnswer());
        imgPath = URLUtils.getImgPath(this, userData.getPhone());
        userNameTv.setText(userData.getNickname());
        userIdTv.setText("贝壳账号: " + String.valueOf(userData.getId()));

        File file = new File(imgPath);
        if (file.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
            userImg.setImageBitmap(bitmap);
        } else {
            userImg.setImageResource(R.mipmap.user);
        }
    }

    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void findView() {
        backImg = findViewById(R.id.setting_back);
        userNameTv = findViewById(R.id.setting_account_userName);
        userIdTv = findViewById(R.id.setting_account_userId);
        userImg = findViewById(R.id.setting_account_userImg);
    }

    // 用户信息栏点击响应
    public void onClickUser(View view) {
        if (userNameTv.getText().equals("请先登录")) {
            Intent intentLogin = new Intent(this, LoginRegActivity.class);
            intentLogin.putExtra("flag", 0);
            startActivity(intentLogin);
        }
        else {
            Intent intentSelfData = new Intent(this, SelfDataChangeActivity.class);
            startActivity(intentSelfData);
        }
    }

    // 收货地址栏点击响应
    public void onClickAddress(View view) {
        EditDialog dialog = new EditDialog(this);
        dialog.show();
        dialog.setMyTitle("收货地址")
                .setEditText(userData.getAddress())
                .setOnClickBottomListener(new EditDialog.OnClickBottomListener() {
                    @Override
                    public void onPositiveClick() {
                        String Address = dialog.getEditText();
                        if (!Address.equals("")) {
                            newUserData.setAddress(Address);
                            Map<String, String> map = new HashMap<>();
                            map.put("id", String.valueOf(newUserData.getId()));
                            map.put("address", newUserData.getAddress());
                            map.put("nickname", newUserData.getNickname());
                            map.put("name", newUserData.getName());
                            postNetwork(URLUtils.updateInfo_url, map);          // 修改服务器数据
                        }
                        dialog.dismiss();
                    }
                    @Override
                    public void onNegtiveClick() {
                        dialog.dismiss();
                    }
                });
    }

    // 修改密码栏点击响应
    public void onClickChangePwd(View view) {
        Intent intent = new Intent(SettingActivity.this, NewPwdActivity.class);
        startActivity(intent);
    }

    // 修改密保栏点击响应
    public void onClickChangeQues(View view) {
        Intent intent = new Intent(SettingActivity.this, NewPwdActivity.class);
        startActivity(intent);
    }

    // 关于栏点击响应
    public void onClickAbout(View view) {
        Intent intent = new Intent(SettingActivity.this, AboutActivity.class);
        startActivity(intent);
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:       // 修改成功
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                // 保存修改信息
                SaveUserUtils.saveUserToFile(this, newUserData);
                refreshData();
                break;
            default:        // 其他，修改失败
                Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}